/*
 * ===================================================================
 *  
 *      Filename:  log_client_nic_info.c
 *
 *   Description:  This file is used to get the NIC(network interface card)
 *                 information.We mainly focus on the address of the NIC.
 *
 *       Version:  1.0
 *        Create:  2010/07/13
 *
 *        Author:  Keven Zhu     kevenzhu@tencent.com
 *     Copyright:  Reserved by Tencent.com Co.ltd
 *          
 * ====================================================================
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h>

int get_nic_list (struct ifreq* buf )
{
	int fd, intrface,return_num = 0;
	struct ifconf ifc;

	if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
	{
		ifc.ifc_len = sizeof(struct ifreq) * 16;
		ifc.ifc_buf = (caddr_t) buf;
		if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))
		{
			intrface = ifc.ifc_len / sizeof (struct ifreq);
            return_num = intrface;
            #if 1
			while (intrface-- > 0)
			{
				if (!(ioctl (fd, SIOCGIFFLAGS, (char *) (buf+ intrface))))
				{
                    /*get the flags info*/
				}
				else
				{
					perror ("failed to get nic");
                    return 0;
				}
                /*parse the flags*/
				if ((buf +intrface)->ifr_flags & IFF_UP)
				{
				    if (!(ioctl (fd, SIOCGIFADDR, (char *) (buf+intrface))))
				    {
                        /*get the ip address*/;
				    }
				    else
				    {
                        /*set the address to zero*/
                        (buf+intrface)->ifr_flags = 0;
					    perror ("failed to get the ip");
				    }
			    }
				else
				{
                    ((struct sockaddr_in*)(&((buf +intrface)->ifr_addr)))->sin_addr.s_addr = 0;
				}
			}
            #endif
            return return_num;
		}
        else
        {
            return 0;
        }
	}
    else
    {
        return 0;
    }
}



